import collections
import heapq
import os
import sys
import time
from typing import List

import yaml
from appium.webdriver.common.touch_action import TouchAction
from selenium.webdriver.common.by import By
from Base import base_driver
from appium import webdriver


# driver = base_driver.base_driver("vivoY55A", True)
# time.sleep(5)
# action = TouchAction(driver)
# a = driver.find_element_by_xpath("//*[contains(@text, '调试')]")
# print(a)
# s = a.location
# # action.long_press(el=a, duration=3000)
# action.press(el=a, x=None, y=None).move_to(x=s['x'] + 30, y=s['y']+300).release().perform()

# driver = base_driver.base_driver("OnePlus", True)
# action = TouchAction(driver)
# driver.find_element_by_xpath("//*[contains(@text, '查产品')]").click()
# driver.find_element_by_id("org.c2h4.afei.beauty:id/et_container").click()
# time.sleep(2)
# print(driver.get_window_size())
# s = driver.get_window_size()
# time.sleep(2)
# time.sleep(2)
# action = TouchAction(driver)
# action.tap(x=270, y=1150).perform()
# # driver.find_element_by_id("android:id/inputArea").click()
# # driver.find_element_by_id("miui:id/search_container").click()
# driver.find_element_by_class_name("android.widget.Button").click()
# driver.find_element_by_id("com.android.mms:id/embedded_text_editor").click()
# driver.find_element_by_class_name("android.widget.EditText").send_keys("验证码")
# s = driver.get_window_size()
# print(s)
# action.tap(x=s['width']-30, y=s['height']-30).perform()
# driver.set_clipboard()
# driver.set_clipboard_text()
# time.sleep(5)
# driver.close_app()
# driver.quit()


# with open(r"D:\app_auto_test\Base\driver_info.yaml", "r", encoding="utf-8") as f:
#     data = yaml.load(f)
# print(data)
# app_launch = (By.XPATH, "//*[contains(@text,'你今天真好看')]")  # app
# skip_loc = (By.ID, "org.c2h4.afei.beauty:id/cdpv_time")  # 跳过
# swipe_right = (500, 1000, 200, 1000, 500)
#
# driver = base_driver.launch_driver("RedMi", True)
# driver.get_window_size()
# while True:
#     try:
#         driver.find_element_by_xpath("//*[contains(@text,'你今天真好看')]").click()
#         break
#     except:
#         driver.swipe(*swipe_right)
#
# # file_name = sys._getframe().f_code.co_name
# time.sleep(1)
# print(len(driver.find_elements_by_id("org.c2h4.afei.beauty:id/cdpv_time")))
# time_stamp = str(time.time()).split(".")[0]
# driver.get_screenshot_as_file(r"D:\app_auto_test\ScreenShot" + os.sep + time_stamp + ".png")
# time.sleep(3)


# 短信验证码登录
# driver = base_driver.base_driver("RedMi", True)
# driver.find_element_by_id("org.c2h4.afei.beauty:id/iv_mine").click()
# driver.find_element_by_id("org.c2h4.afei.beauty:id/mine_tv_name").click()
# driver.find_element_by_xpath("//*[contains(@text, '手机登录')]").click()
# driver.find_element_by_id("org.c2h4.afei.beauty:id/tv_phone_sms").click()
# driver.find_element_by_id("org.c2h4.afei.beauty:id/et_num").send_keys(18668149508)
# driver.find_element_by_id("org.c2h4.afei.beauty:id/tv_tip").click()
# time.sleep(10)
# driver.open_notifications()
# text = driver.find_element_by_xpath("//*[contains(@text,'验证码')]").text[-4:]
# driver.keyevent(4)
# driver.find_element_by_id("org.c2h4.afei.beauty:id/et_vertify").send_keys(text)
# time.sleep(2)
# driver.find_element_by_id("org.c2h4.afei.beauty:id/tv_submit").click()

# desired_caps = {}
# desired_caps["platformName"] = "android"
# desired_caps["platformVersion"] = "10"
# desired_caps["udid"] = "d6c8af19"
# desired_caps["deviceName"] = "d6c8af19"
# desired_caps["appPackage"] = "org.c2h4.afei.beauty"
# # desired_caps["appActivity"] = ".splash.SplashActivity"
# desired_caps["noReset"] = True
# driver = webdriver.Remote("http://127.0.0.1:4725/wd/hub", desired_caps)
# action = TouchAction(driver)
# action.tap(x=500, y=1000).perform()
# time.sleep(5)
# driver.close_app()
# driver.quit()
# swipe_down = (500, 1500, 500, 500, 500)
# s = driver.find_elements(By.ID, "org.c2h4.afei.beauty:id/iv_mine")
# driver.find_element_by_id("org.c2h4.afei.beauty:id/main_ib_camera").click()
# driver.find_element_by_id("org.c2h4.afei.beauty:id/btn_self_test").click()
# for i in range(3):
#     driver.swipe(*swipe_down)
# driver.find_element_by_id("org.c2h4.afei.beauty:id/tv_begin_test").click()
# driver.find_element_by_xpath("//*[contains(@text, '品牌库')]").click()
# driver.find_elements_by_id("org.c2h4.afei.beauty:id/iv_banner")[0].click()
#
# driver.find_element_by_id("org.c2h4.afei.beauty:id/ll_go_brand").click()
# driver.find_elements_by_id("org.c2h4.afei.beauty:id/tv_shop_name")[1].click()
# time.sleep(2)
# driver.swipe(500, 2000, 500, 500)
# while True:
#     try:
#         if driver.find_element_by_xpath("//*[contains(@text, '已切换至完整浏览模式')]"):
#             break
#     except:
#         driver.find_elements_by_class_name("android.view.View")[1].click()

# while True:
#     try:
#         buy = driver.find_element_by_xpath("//*[contains(@text, '领券购买')]")
#         if buy:
#             buy.click()
#             break
#     except:
#         driver.swipe(500, 1000, 500, 500, 500)

# s = str(time.time()).split(".")[0]
# s = s + ".png"
# print(s)
# def a():
#     print(sys._getframe().f_code.co_name)

# class Solution:
#     def networkDelayTime(self, times: List[List[int]], n: int, k: int) -> int:
#         graph = collections.defaultdict(dict)
#         for u, v, w in times:
#             graph[u][v] = w
#         # print(graph.items())
#         queue = [(0, k)]
#         visted = set()
#         while queue:
#             t, node = heapq.heappop(queue)
#             print(node)
#             if node in visted:
#                 continue
#             visted.add(node)
#             if len(visted) == n:
#                 return t
#             for v, w in graph[node].items():
#                 if v not in visted:
#                     print(v, w, (w + t, v))
#                     heapq.heappush(queue, (w + t, v))
#             print(queue)
#             print(visted)
#             print("==================================")
#         return -1
#
#
# if __name__ == '__main__':
#     time = [[3, 5, 78], [2, 1, 1], [1, 3, 0], [4, 3, 59], [5, 3, 85], [5, 2, 22], [2, 4, 23], [1, 4, 43], [4, 5, 75],
#             [5, 1, 15], [1, 5, 91], [4, 1, 16], [3, 2, 98], [3, 4, 22], [5, 4, 31], [1, 2, 0], [2, 5, 4], [4, 2, 51],
#             [3, 1, 36], [2, 3, 59]]
#     n = 5
#     k = 5
#     sol = Solution()
#     print(sol.networkDelayTime(time, n, k))
